package com.sphd.Restaurant.fragments.orderFragments;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.activity.IndentDetailsActivity;
import com.sphd.Restaurant.base.LazyLoadFragment;
import com.sphd.Restaurant.infos.IndentInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.InOrOutOrdersP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.InOrOutOrdersView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2015/12/7.
 */
public class InOrdersFragment extends LazyLoadFragment implements InOrOutOrdersView {

    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    InOrOutOrdersP inoroutP;
    private int curpage=1;
    BaseQuickAdapter<IndentInfo> baseQuickAdapter;
    List<IndentInfo> indentInfos;
    @Override
    public int getLayout() {
        return R.layout.fragment_coupons_cards;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySrf.setOnRefreshListener(() -> {
            if (getBoolean(Constants.ISLOGIN)){
                curpage=1;
                loadData();
            }else {
                mySrf.setRefreshing(false);
            }
        });
        baseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                curpage++;
                loadData();
            }
        });
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle =new Bundle();
                bundle.putInt(Constants.TYPE,0);
                bundle.putSerializable(Constants.INDENT,(IndentInfo)baseQuickAdapter.getItem(position));
                startActivity(IndentDetailsActivity.class,bundle);
            }
        });
    }

    private void initView() {
        indentInfos=new ArrayList<>();
        setRefresh(mySrf);
        setRecy(myRecy);
        baseQuickAdapter=new BaseQuickAdapter<IndentInfo>(R.layout.item_fragment_in_out,indentInfos) {
            @Override
            protected void convert(BaseViewHolder helper, IndentInfo item) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                helper.setText(R.id.cg_name,item.getOrder_cg())
                        .setText(R.id.py_money,"消费金额" + item.getOrder_lastprice() + "元")
                        .setText(R.id.py_fs,item.getOrder_pay()==0?"现金支付":"充值卡支付")
                        .setText(R.id.pay_time,format.format(new Date(item.getOrder_fintime())));

            }
        };
        baseQuickAdapter.openLoadAnimation();
        baseQuickAdapter.openLoadMore(10);
        myRecy.setAdapter(baseQuickAdapter);
        inoroutP=new InOrOutOrdersP(this);
    }

    @Override
    public void loadData() {
        if (getBoolean(Constants.ISLOGIN))
        inoroutP.addData(Model.HTTPURL + Model.DINGDAN + "username=" + getString(Constants.USERPHONE) + "&order_isosale=0&curpage=" +
                curpage + "&pageno=10");
    }

    @Override
    public void sendResList(List<IndentInfo> infos) {
        if (curpage==1){
            baseQuickAdapter.setNewData(infos);
        }else {
            baseQuickAdapter.addData(infos);
        }
        if (infos.size()!=10){
            baseQuickAdapter.loadComplete();
            addFootView(baseQuickAdapter);
        }
        setLoadDataCompleted(true);
    }

    @Override
    public void showdialog() {
showDialog();
    }

    @Override
    public void dissmissdialog() {
            dissMissDialog();
        mySrf.setRefreshing(false);
    }
}
